/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vpkmanager;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author jquick
 */
public class VpkNode {
    
    public VpkNode() {
        this.parent = null;     
    }
    
    public VpkNode(VpkNode parent) {
        this.parent = parent;
    }
    
    VpkFile vpf;
    
    private final VpkNode parent;
    private final int terminator = 0xFFFFFFFF;
    
    //node properties
    protected String name;
    protected int crc;
    protected int entryOffset;
    protected int entryLength;
    protected short preloadBytes;
    protected short archiveIndex;
    protected byte[] preloadData;
    protected VpkNode[] children;
    protected int index;
    
    protected int load(BufferedInputStream reader, int index) throws IOException {
        
        List<Byte> bytes = new ArrayList<Byte>();
        byte nextByte;
        do {
            nextByte = (byte)reader.read();
            bytes.add(nextByte);
        } while (nextByte != '\0');
        byte[] bytearr = new byte[bytes.size()];
        for(int i = 0; i <= bytes.size()-1; i = i+1)
            bytearr[i] = bytes.get(i);
        this.name = new String(bytearr,"UTF-8").trim();
        return bytes.size();
    }
    
    protected int loadFileInfo(BufferedInputStream reader, VpkFile vpf) throws IOException {
        vpf.index += load(reader, vpf.index);
        if (name == null || name.isEmpty()) 
            return vpf.index;
        
        this.index = vpf.index - name.length()-1;
        this.crc = vpf.readInt(reader);
        this.preloadBytes = vpf.readShort(reader);
        this.archiveIndex = vpf.readShort(reader);
        this.entryOffset = vpf.readInt(reader);
        this.entryLength = vpf.readInt(reader);

        short tmpterm = vpf.readShort(reader);
        if (tmpterm != terminator)
            throw new InvalidClassException("Error: VPK entry did not end with correct terminator");

        if (preloadBytes > 0) {
                this.preloadData = new byte[preloadBytes];
                for(int i = 0; i < preloadBytes; i = i+1) {
                    this.preloadData[i] = (byte)reader.read();
                } 
        }
        return vpf.index + preloadBytes;
    }
    
    public VpkNode getParent() {
        return this.parent;
    }
    
    public String getFilePath() {
        if (parent != null && parent.getParent() != null) {
                String fileName = name;
                String path = parent.name;
                String extension = parent.getParent().name;

                return String.format("%s/%s.%s", path, fileName, extension);
        }
        return null;
    }
    
    public String getFileDirectory() {
        if (parent != null) {
                String path = parent.name;
                return String.format("%s/", path);
        }
        return null;
    }
    
    public String getFileName() {
        if (parent != null && parent.getParent() != null) {
            String fileName = name;
            String extension = parent.getParent().name;

            return String.format("%s.%s", fileName, extension);
        }
        return null;
    }
}
